package org.ww.edu.controller;


import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.ww.commonutils.R;
import org.ww.edu.bean.Chapter;
import org.ww.edu.bean.vo.ChapterVo;
import org.ww.edu.service.ChapterService;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author ww
 * @since 2020-10-23
 */
@RestController
@RequestMapping("/edu/chapter")
@Api(description = "章节小节管理")
public class ChapterController {

    @Autowired
    private ChapterService chapterService;

    /**
     * 获取所有的章节小节信息
     */
    @GetMapping("getAllChapterVideo/{courseId}")
    public R getAllChapterVideo(@PathVariable String courseId){
        List<ChapterVo> list = chapterService.getAllChapterVideo(courseId);
        return R.ok().data("chapterVideo",list);
    }
    /**
     * 添加章节信息
     */
    @PostMapping("/addChapter")
    public R addChapter(@RequestBody Chapter chapter){
        chapterService.save(chapter);
        return R.ok();
    }

    /**
     * 根据id获取章节信息
     * @param chapterId
     * @return
     */
    @GetMapping("/{chapterId}")
    public R getChapter(@PathVariable String chapterId){
        Chapter chapterInfo = chapterService.getById(chapterId);
        return R.ok().data("chapterInfo",chapterInfo);
    }
    /**
     * 根据id修改章节信息
     */
    @PostMapping("/updateChapter")
    public R updateChapter(@RequestBody Chapter chapter){
        chapterService.updateById(chapter);
        return R.ok();
    }
    /**
     * 删除章节信息
     */
    @DeleteMapping("/{chapterId}")
    public R deleteChapter(@PathVariable String chapterId){
        boolean flag=chapterService.deleteChapter(chapterId);
        if(flag){
            return R.ok();
        }else{
            return R.error();
        }
    }
}

